package com.yeban.javase.day12.test.streamtest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/22
 *
 * 8.将员工按照工资由低到高排序，显示名字
 */
public class StreamTest08 {

    public static void main(String[] args) {
        List<Employee2> employees = Arrays.asList (
                new Employee2 ("张三", 7500.0),
                new Employee2 ("李四", 8200.0),
                new Employee2 ("王五", 9000.0),
                new Employee2 ("赵六", 7800.0),
                new Employee2 ("孙七", 10000.0));
        // sorted方法
        System.out.println("---------- sorted方法 ----------");
        employees.stream().sorted(Comparator.comparingDouble(Employee2::getSalary))
                          .map(Employee2::getName)
                          .forEach(System.out::println);

        // 使用lambda自定义比较器
        System.out.println("---------- lambda自定义比较器 ----------");
        employees.stream().sorted((e1, e2) -> e1.getSalary().compareTo(e2.getSalary()))
                .map(Employee2::getName)
                .forEach(System.out::println);

        // 方法引用
        System.out.println("---------- 方法引用 ----------");
        employees.stream().sorted(Comparator.comparing(Employee2::getSalary))
                .map(Employee2::getName)
                .forEach(System.out::println);

        // Employee2 实现 Comparable 接口
        System.out.println("---------- Employee2 实现 Comparable 接口 ----------");
        employees.stream()
                .sorted()
                .map(Employee2::getName)
                .forEach(System.out::println);
    }
}

class Employee2 implements Comparable<Employee2> {
    private String name;
    private Double salary;

    public Employee2() {
    }

    public Employee2(String name, Double salary) {
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int compareTo(Employee2 o) {
        return this.salary.compareTo(o.salary);
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return salary
     */
    public Double getSalary() {
        return salary;
    }

    /**
     * 设置
     * @param salary
     */
    public void setSalary(Double salary) {
        this.salary = salary;
    }

    public String toString() {
        return "Employee{name = " + name + ", salary = " + salary + "}";
    }
}

